import store from '@/store'
// import {getPartnerList, getMealPlanList} from "@/api/organ/business";
import { getAreaList} from "@/api/organ/dropDown"
export default {
  state: {
    partnerList: [],                               // 交伙类型
    mealPlanList: [],                              // 食谱列表,
    areaList: [],                                  // 区域列表
    mealList: [                                    // 餐次列表
      {id: 0, name: '早餐'}, {id: 1, name: '午餐'}, {id: 2, name: '晚餐'}
    ]
  },
  mutations: {
    setPartnerList(state, data) {state.partnerList = data},
    setMealPlanList(state, data) {state.mealPlanList = data},
    setareaList(state, data) {state.areaList = data},
  },
  getters: {
    // 交伙类型
    d_partnerList(state, getters, rootState) {
      return function (v) {
        if (!state.partnerList.length) store.dispatch('getPartnerListApi', v)
        return state.partnerList;
      }
    },
    // 食谱列表
    d_mealPlanList(state, getters, rootState) {
      if (!state.mealPlanList.length) store.dispatch('getMealPlanListApi')
      return state.mealPlanList;
    },
    d_areaList(state, getters, rootState) {
      store.dispatch('getAreaListApi')
      return state.areaList;
    },
    // 食谱列表
    d_mealList(state, getters, rootState) {
      return state.mealList;
    },
  },
  actions: {
    getPartnerListApi({commit}, v) {
      getPartnerList({current: 1, size: 1000, settingType: v}).then(res => {
        if (res.errorcode === 0) {
          res.foodSettingList =  res.foodSettingList.map(val => {
            val.name = val.personnelType;
            val.id = val.personnelType;
            return val;
          })
          console.log(res.foodSettingList);
          commit('setPartnerList', res.foodSettingList)
        }
      })
    },
    getMealPlanListApi({commit}) {
      getMealPlanList ({current: 1, size: 1000}).then(res => {
        if (res.retCode === 2000) {
          res.data = res.data.map(v => {
            v.id = v.mealPlanId;
            return v;
          })
          commit('setMealPlanList', res.data)
        }
      })
    },
      // 获取区域列表
      getAreaListApi({commit}) {
          getAreaList({}).then((res) => {
            if(res.code == 1) {
              console.log('111111',res.data);
              commit('setareaList', res.data)
            }
          })
  
      },
  }
}
